package com.itheima.controller;


import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfoVo;
import com.itheima.service.OrderService;
import com.itheima.utils.JedisUtil;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    JedisUtil jedisUtil;
    @Reference
    OrderService orderService;

    @RequestMapping("/submit")
    public Result submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        String token = orderInfoVo.getToken();
        //从map中获取用户输入的验证码
        String validateCode = orderInfoVo.getValidateCode();
        //从页面中获取用户输入的电话号码
        String telephone = orderInfoVo.getTelephone();
        //从redis中获取缓存的验证码  根据设置的标识
        String codeInRedis = jedisUtil.get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //判断验证码是否正确
        if (codeInRedis == null && !codeInRedis.equals(validateCode)) {
            return  Result.error("验证码无效");
        }
        orderInfoVo.setOrderType(Order.ORDERTYPE_WEIXIN);
        return orderService.submit(orderInfoVo);
    }


    @RequestMapping("/findById")
    public Result findById(Integer id){
        Map map=orderService.findById(id);
        return  Result.success("",map);
    }
    @RequestMapping("/getToken")
    public Result getToken(){
        String token = UUID.randomUUID().toString();
        jedisUtil.setex(token,60 * 60 ,token);
        return Result.success("",token);
    }
}
